package com.xry.test1.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xry.test1.model.LightData;
import com.xry.test1.service.MQTTSubscriber.MQTTSubscribe;
import com.xry.test1.model.LightControlRequest;
import com.xry.test1.service.impl.LightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;




//@RestController
//@RequestMapping("/bedroom_light")
//public class LightController {
//
//    private final LightService lightService;
//
//    public LightController(LightService lightService) {
//        this.lightService = lightService;
//    }
//
//    @PostMapping("/control")
//    public String controlLight(@RequestBody LightControlRequest request) {
//        int lightIndex = request.getLight() - 1; // 1 → 0, 2 → 1, 3 → 2
//        return lightService.controlLight(lightIndex, request.getAction());
//    }
//}

//@RestController
//@RequestMapping("/light")
//public class LightController {
//
//    @Autowired
//    private LightService lightService;
//
//    @PostMapping("/control")
//    public String controlLight(@RequestBody LightControlRequest request) {
//        String newLightState = updateLightState(request);  // 根据请求更新灯的状态
//
//        // 将新的状态推送到 MQTT 的 bedroom_cmd 主题
//        mqttSubscribe.sendMQTTMessage("bedroom_cmd", newLightState, 2);
//
//        // 将新的灯状态推送到 WebSocket 给前端（前端需要更新按钮状态）
//        lightService.sendInitialStateToFrontend();
//
//        return "灯状态已更新";
//    }
//
//    // 根据请求更新灯的状态
//    private String updateLightState(LightControlRequest request) {
//        String currentState = lightService.getCurrentLightState(); // 获取当前状态
//        char[] stateArray = currentState.toCharArray();
//
//        // 更新灯状态
//        stateArray[request.getLight() - 1] = request.getAction().equals("on") ? '1' : '0';
//
//        // 返回新的灯状态
//        return new String(stateArray);
//    }
//}

@RestController
@RequestMapping("/api/light")
public class LightController {

    @Autowired
    private LightService lightService;

    /**
     * 获取灯的当前状态
     */
//    @GetMapping("/state")
//    public String getLightState() {
//        return lightService.getLightState();
//    }
    @GetMapping("/state")
    public LightData getLightState() {
        return lightService.getLightState();
    }

    /**
     * 控制灯的状态
     */
//    @PostMapping("/control")
//    public String controlLight(@RequestBody LightControlRequest request) {
//        int lightIndex = request.getLight() - 1; // 1 → 0, 2 → 1, 3 → 2
//        boolean success = lightService.controlLight(lightIndex, "on".equals(request.getAction()));
//        return success ? "success" : "failed";
//    }
    @PostMapping("/control")
    public ResponseEntity<String> controlLight(@RequestBody LightControlRequest request) {
        boolean success = lightService.controlLight(request.getLight(), request.getAction());
        return success ? ResponseEntity.ok("OK") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("ERROR");
    }
}


